//
//  TravelDayController.m
//  We_Travel
//
//  Created by lanou3g on 15/12/11.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "TravelDayController.h"
#import "MapViewController.h"
#import "AddViewCell.h"
#import "ArrangeCell.h"
#import "SortViewController.h"
#import "NH_DataDownloadTools.h"
#import "url.h"
#import "SiteRecomendModel.h"
#import "CompleteItineratyController.h"
#import "CoreDataToolHelper.h"
#import "SiteManager.h"
#import "WMPageController.h"
#import "Site.h"

/**
 *  选择项目各文件
 *
 */
#import "AttractionsTableViewController.h"
#import "SelectHotelTableViewController.h"
#import "ExperienceTableViewController.h"
#import "MealTableViewController.h"
#import "ShopTableViewController.h"
#import "TrafficTableViewController.h"



@interface TravelDayController ()<BMKMapViewDelegate,UITableViewDataSource,UITableViewDelegate>




//section展开,收缩
@property (nonatomic,assign) NSInteger didSection;
@property (nonatomic,assign) NSInteger endSection;
@property (nonatomic,assign) BOOL ifClose;
@property (nonatomic,strong) NSMutableArray *flagArray;

@property (weak, nonatomic) IBOutlet UIToolbar *toolBar;

//自适应高度用
@property (nonatomic,strong) NSString *string;
@end

@implementation TravelDayController
- (void)viewWillAppear:(BOOL)animated{
    [_mapView viewWillAppear];
    _mapView.delegate = self;
    [self.tableView reloadData];
}
- (void)viewWillDisappear:(BOOL)animated{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.toolBar.hidden = NO;
    [self setupTableView];
    [self setupNavigation];
    [[SiteManager shareSiteManager]setResultBlock:^{
        [self.tableView reloadData];
    }];
    [[SiteManager shareSiteManager]requireDataIsRecomend:self.isRecomend RecomendSiteID:self.siteID];
}
- (IBAction)didClickButton:(UIBarButtonItem *)sender {
    //site保存到数据库
    [[CoreDataToolHelper shareCoreDataTool]deleteTravelSiteDay:self.siteDay AreaCount:self.areaCount CountryCount:[SiteManager shareSiteManager].countryCount];
    for (SiteRecomendModel *model in [SiteManager shareSiteManager].mornArray) {
        [[CoreDataToolHelper shareCoreDataTool]addTravelSiteName:model.name siteID:model.mtu_id Sitetime:@"am" SiteDay:self.siteDay AreaCount:self.areaCount CountryCount:[SiteManager shareSiteManager].countryCount];
    }
    for (SiteRecomendModel *model in [SiteManager shareSiteManager].afteArray) {
        [[CoreDataToolHelper shareCoreDataTool]addTravelSiteName:model.name siteID:model.mtu_id Sitetime:@"pm" SiteDay:self.siteDay AreaCount:self.areaCount CountryCount:[SiteManager shareSiteManager].countryCount];
    }
    for (SiteRecomendModel *model in [SiteManager shareSiteManager].evenArray) {
        [[CoreDataToolHelper shareCoreDataTool]addTravelSiteName:model.name siteID:model.mtu_id Sitetime:@"night" SiteDay:self.siteDay AreaCount:self.areaCount CountryCount:[SiteManager shareSiteManager].countryCount];
    }
    [[SiteManager shareSiteManager].mornArray removeAllObjects];
    [[SiteManager shareSiteManager].afteArray removeAllObjects];
    [[SiteManager shareSiteManager].afteArray removeAllObjects];
    CompleteItineratyController *ciVC = [[CompleteItineratyController alloc]init];
    [self.navigationController showViewController:ciVC sender:nil];
}
//设置navigation
- (void)setupNavigation{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction:)];
    self.navigationController.navigationBar.barTintColor = [UIColor cyanColor];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"排序" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonItemAction:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
}
- (void)setupTableView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"AddViewCell" bundle:nil]forCellReuseIdentifier:@"addCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ArrangeCell" bundle:nil]forCellReuseIdentifier:@"arrange"];
}
- (void)leftBarButtonItemAction:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)rightBarButtonItemAction:(UIBarButtonItem *)sender{
    SortViewController *sort = [[SortViewController alloc]init];
//    [sort setSendDataBlock:^(NSMutableArray *dataArray) {
//        self.mornArray = dataArray[0];
//        self.afteArray = dataArray[1];
//        self.evenArray = dataArray[2];
//    }];
    sort.navigationItem.hidesBackButton = YES;
    [self.navigationController showViewController:sort sender:nil];
}

#pragma mark -- tableView datasource delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        if (section == 0) {
            return [SiteManager shareSiteManager].mornArray.count * 2+1;
        }else if (section == 1){
            return [SiteManager shareSiteManager].afteArray.count * 2+1;
        }else{
            return [SiteManager shareSiteManager].evenArray.count * 2+1;
        }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row % 2) {
        ArrangeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"arrange"];
        if (indexPath.section == 0) {
            SiteRecomendModel *model = [SiteManager shareSiteManager].mornArray[indexPath.row/2];
            cell.model = model;
            self.string =cell.detailLabel.text;
        }
        if (indexPath.section == 1) {
            SiteRecomendModel *model = [SiteManager shareSiteManager].afteArray[indexPath.row/2];
            cell.model = model;
             self.string =cell.detailLabel.text;
        }
        if (indexPath.section == 2) {
            SiteRecomendModel *model = [SiteManager shareSiteManager].evenArray[indexPath.row/2];
             cell.model = model;
             self.string =cell.detailLabel.text;
        }
        CGSize size = CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds) - 40, 2000);
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
        CGRect rect = [self.string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil];
        return rect.size.height + 190;
    }else{
        return 30;
    }
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row%2) {
        ArrangeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"arrange"forIndexPath:indexPath];
        if (indexPath.section == 0) {
            SiteRecomendModel *model = [SiteManager shareSiteManager].mornArray[indexPath.row/2];
            cell.model = model;
        }
        if (indexPath.section == 1) {
            SiteRecomendModel *model = [SiteManager shareSiteManager].afteArray[indexPath.row/2];
            cell.model = model;
        }
        if (indexPath.section == 2) {
            SiteRecomendModel *model = [SiteManager shareSiteManager].evenArray[indexPath.row/2];
            cell.model = model;
        }
        return cell;
    }else{
        AddViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addCell"forIndexPath:indexPath];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 40)];
    headView.backgroundColor  = [UIColor whiteColor];
    self.headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 5, 30, 30)];
    self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.headerImageView.frame) + 10, 0, 50, 40)];
    self.timeIntervelLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.timeLabel.frame) + 10, 0, 100, 40)];
    self.timeIntervelLabel.font = [UIFont systemFontOfSize:14];
    self.timeIntervelLabel.text = @"2-3小时";
    [headView addSubview:self.headerImageView];
    [headView addSubview:self.timeLabel];
    [headView addSubview:self.timeIntervelLabel];
    if (section == 0) {
        self.timeLabel.text = @"上午";
        self.headerImageView.image = [UIImage imageNamed:@"morning.png"];
    }else if (section == 1) {
        self.timeLabel.text = @"下午";
        self.headerImageView.image = [UIImage imageNamed:@"afternoon.png"];
    }else if (section == 2) {
        self.timeLabel.text = @"晚上";
        self.headerImageView.image = [UIImage imageNamed:@"evening.png"];
    }
    return headView;
}
/*
//- (void)deletCell:(UIButton *)sender{
//    self.endSection = sender.tag - 100;
//    if (self.didSection == 4) {
//        self.didSection = self.endSection;
//        [self didSelectCellRowFirstDo:YES SecondDo:NO];
//    }else{
//        if (self.didSection == self.endSection) {
//            [self didSelectCellRowFirstDo:NO SecondDo:NO];
//        }else{
//             [self didSelectCellRowFirstDo:NO SecondDo:YES];
//        }
//    }
//}

//- (void)didSelectCellRowFirstDo:(BOOL)firstDo SecondDo:(BOOL)secondDo{
//    [self.tableView beginUpdates];
//    NSMutableArray* rowToInsert = [[NSMutableArray alloc] init];
//    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:self.didSection];
//    [rowToInsert addObject:indexPath];
//    NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:1 inSection:self.didSection];
//    [rowToInsert addObject:indexPath1];
//    NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:2 inSection:self.didSection];
//    [rowToInsert addObject:indexPath2];
//    NSIndexPath *indexPath3 = [NSIndexPath indexPathForRow:3 inSection:self.didSection];
//    [rowToInsert addObject:indexPath3];
//    NSIndexPath *indexPath4 = [NSIndexPath indexPathForRow:4 inSection:self.didSection];
//    [rowToInsert addObject:indexPath4];
//    self.ifClose = firstDo;
//    if (self.ifClose) {
//        [self.tableView deleteRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationFade];
//        self.iconImageView.image = [UIImage imageNamed:@"open.png"];
//       
//    }else{
//        self.didSection = 4;
//        [self.tableView insertRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationFade];
//        self.iconImageView.image = [UIImage imageNamed:@"close.png"];
//       
//    }
//    [self.tableView endUpdates];
//    if (secondDo){
//        self.didSection = self.endSection;
//        [self didSelectCellRowFirstDo:YES SecondDo:NO];
//    }
//    //[self.tableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES];
//}
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row%2) {
        
    }else{
        [SiteManager shareSiteManager].indexPath = indexPath;
        WMPageController *pageVC = [self p_defaultController];
        [self.navigationController showViewController:pageVC sender:nil];
    }
}
- (WMPageController *)p_defaultController {
    NSArray *viewControllers = @[[AttractionsTableViewController class], [SelectHotelTableViewController class], [ExperienceTableViewController class],[MealTableViewController class],[ShopTableViewController class],[TrafficTableViewController class]];
    NSArray *titles = @[@"景点", @"住宿", @"体验",@"餐饮",@"购物",@"交通"];
    WMPageController *pageVC = [[WMPageController alloc] initWithViewControllerClasses:viewControllers andTheirTitles:titles];
    /**
     *   选中的字体颜色
     */
    pageVC.titleColorSelected = [UIColor cyanColor];
    /**
     *  选中的外框颜色
     */
    pageVC.progressColor = [UIColor blueColor];
    pageVC.pageAnimatable = YES;
    pageVC.menuItemWidth = 55;
    pageVC.postNotification = YES;
    pageVC.bounces = YES;
    return pageVC;
}

#pragma mark -- bmkMapViewDelegate
- (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate{
    MapViewController *mvc = [[MapViewController alloc]init];
    [self.navigationController showViewController:mvc sender:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
